package com.wnxy.portal.rabbit.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitCancelOrderConfig {

    // 普通队列: order-queue
    // 普通交换机: order-exchange
    // 队列绑定到交换机：order.key.normal
    @Bean
    public Queue orderQueue() {
        return QueueBuilder.durable("order-queue") // 创建持久化的队列，可以确保消息不丢失
                .deadLetterExchange("order-dlx-exchange") // 普通队列绑定死信交换机
                .deadLetterRoutingKey("order.key.dlx") // 绑定死信路由key
                .ttl(40 * 60 * 1000) // 设置消息过期时间: 40分钟 / 1 分钟
                .maxLength(30000)  // 设置队列最大长度
                .build();
    }

    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange("order-exchange");
    }

    @Bean
    public Binding bindNormalQueueToExchange(Queue orderQueue, DirectExchange orderExchange) {
        return BindingBuilder.bind(orderQueue).to(orderExchange).with("order.key.normal");
    }

    // 死信队列: order-dlx-queue
    // 死信交换机: order-dlx-exchange
    // 死信队列绑定到交换机：order.key.dlx
    @Bean
    public Queue orderDlxQueue() {
        return new Queue("order-dlx-queue");
    }

    @Bean
    public DirectExchange orderDlxExchange() {
        return new DirectExchange("order-dlx-exchange");
    }

    @Bean
    public Binding bindDlqToExchange(Queue orderDlxQueue, DirectExchange orderDlxExchange) {
        return BindingBuilder.bind(orderDlxQueue).to(orderDlxExchange).with("order.key.dlx");
    }

}
